#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int date(int year, int month)
{
    int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
    {
        return arr[month] + 1;
    }
    else {
        return arr[month];
    }
}
int main() {
    int year;
    int month;
    int day;
    int n;
    int m;
    cin >> m;
    while (m--)
    {
        cin >> year >> month >> day >> n;
        day = n + day;
        while (day > date(year, month))
        {
            day -= date(year, month);
            month++;
            if (month > 12)
            {
                month = 1;
                year++;
            }
            if (day == 0)
            {
                month--;
                day = date(year, month);
            }
        }
        printf("%04d-%02d-%02d\n", year, month, day);
    }
}